from django import forms
from rbac import models
from django.core.exceptions import ValidationError


class UserModelForm(forms.ModelForm):
    confirm_password = forms.CharField(label='确认密码')
    class Meta:
        model = models.UserInfo
        fields = ['name','email','password','confirm_password']
        # widgets = {
        #     'name': forms.TextInput(attrs={'class': 'form-control'}),
        #     'email': forms.TextInput(attrs={'class': 'form-control'}),
        #     'password': forms.TextInput(attrs={'class': 'form-control'}),
        #     'confirm_password': forms.TextInput(attrs={'class': 'form-control'}),
        # }

    #重置父类方法
    def __init__(self,*args,**kwargs):
        super(UserModelForm,self).__init__(*args,**kwargs)
        for name,filed in self.fields.items():
            print(filed)
            filed.widget.attrs['class']='form-control'

    def clean_confirm_password(self):
        """
        检测密码是否一致
        :return:
        """
        password = self.cleaned_data['password']
        confirm_password = self.cleaned_data['confirm_password']
        if password != confirm_password:
            raise ValidationError('两次密码输入不一致')
        return confirm_password




class UpdateModelForm(forms.ModelForm):
    class Meta:
        model = models.UserInfo
        fields = ['name','email']
        # widgets = {
        #     'name': forms.TextInput(attrs={'class': 'form-control'}),
        #     'email': forms.TextInput(attrs={'class': 'form-control'}),
        #     'password': forms.TextInput(attrs={'class': 'form-control'}),
        #     'confirm_password': forms.TextInput(attrs={'class': 'form-control'}),
        # }

    #重置父类方法
    def __init__(self,*args,**kwargs):
        super(UpdateModelForm,self).__init__(*args,**kwargs)
        for name,filed in self.fields.items():
            print(filed)
            filed.widget.attrs['class']='form-control'


class ConfirmpasswordModelForm(forms.ModelForm):
    confirm_password = forms.CharField(label='确认密码')
    class Meta:
        model = models.UserInfo
        fields = ['password','confirm_password']
        # widgets = {
        #     'name': forms.TextInput(attrs={'class': 'form-control'}),
        #     'email': forms.TextInput(attrs={'class': 'form-control'}),
        #     'password': forms.TextInput(attrs={'class': 'form-control'}),
        #     'confirm_password': forms.TextInput(attrs={'class': 'form-control'}),
        # }

    #重置父类方法
    def __init__(self,*args,**kwargs):
        super(ConfirmpasswordModelForm,self).__init__(*args,**kwargs)
        for name,filed in self.fields.items():
            print(filed)
            filed.widget.attrs['class']='form-control'

    def clean_confirm_password(self):
        """
        检测密码是否一致
        :return:
        """
        password = self.cleaned_data['password']
        confirm_password = self.cleaned_data['confirm_password']
        if password != confirm_password:
            raise ValidationError('两次密码输入不一致')
        return confirm_password

